package com.jackie.easyble.ui.adapter

import android.bluetooth.le.ScanResult
import android.content.Context
import android.view.ViewGroup
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.jackie.easyble.R
import com.jackie.easyble.databinding.AdapterMultipleScanResultItemBinding
import com.jackie.easyble.toastL

/**
 * 扫描结果列表适配器(可多选,最多7个)
 */
class MultipleSelectScanResultRecyclerViewAdapter :
    BaseQuickAdapter<ScanResult, MultipleSelectScanResultRecyclerViewAdapter.ViewHolder>() {
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 内部类
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    class ViewHolder(parent: ViewGroup) : DataBindingHolder<AdapterMultipleScanResultItemBinding>(
        R.layout.adapter_multiple_scan_result_item,
        parent
    )

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 属性声明
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 选中的选项位置
     */
    private val checkedPosition = ArrayList<Int>()

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 构造方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    init {
        setOnItemClickListener { _, _, position ->
            if (checkedPosition.contains(position)) {
                checkedPosition.remove(position)
                notifyItemChanged(position)
            } else {
                if (checkedPosition.size >= 7) {
                    context.toastL(R.string.max_select_size_7)
                    return@setOnItemClickListener
                }
                checkedPosition.add(position)
                notifyItemChanged(position)
            }
        }
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 实现方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * 实现此方法，并使用 [holder] 完成 item 视图的操作
     *
     * @param holder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    override fun onBindViewHolder(holder: ViewHolder, position: Int, item: ScanResult?) {

        holder.binding.checkStateCb.isChecked = checkedPosition.contains(position)
        holder.binding.item = item
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        return ViewHolder(parent)
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 公开方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 清除列表数据
     */
    fun clear() {
        checkedPosition.clear()
        while (true){
            if (items.isEmpty()){
                break
            }
            removeAt(0)
        }
    }

    /**
     * 获取选中的设备信息列表
     */
    fun getSelectedScanResultList(): ArrayList<ScanResult> {
        val result = ArrayList<ScanResult>()
        for (position in checkedPosition) {
            val scanResult = items[position]
            result.add(scanResult)
        }
        return result
    }
}